<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <!-- import CSS -->
  <link rel="stylesheet" href="https://cdn.staticfile.org/element-ui/2.15.9/theme-chalk/index.css">
  <style>
    .el-header a{
      color: #666;
      text-decoration: none;
    }
    .el-menu.el-menu--horizontal{
      border: 0;/*去掉导航菜单 自带的边框*/
    }
    .el-table .el-table__cell{
      padding:0;/*去掉表格自带的上下内边距*/
    }
    .el-table .cell{
      white-space: nowrap;/*设置文本不换行*/
      text-overflow: ellipsis;/*设置文本超出时用省略号展示*/
    }
  </style>
</head>
<body>
<div id="app">
  <el-container>
    <el-header height="150px">
      <div style="width: 1200px;margin: 0 auto">
        <img src="imgs/logo.png" style="vertical-align: middle" width="300">
        <a href="/">首页</a><el-divider direction="vertical"></el-divider>
        <a href="">热点资讯</a><el-divider direction="vertical"></el-divider>
        <a href="">商家入驻</a><el-divider direction="vertical"></el-divider>
        <a href="">社会招聘</a><el-divider direction="vertical"></el-divider>
        <a href="">校园招聘</a><el-divider direction="vertical"></el-divider>
        <a href="">帮助</a>
      </div>
      <div style="background-color: #0aa1ed">
        <el-menu style="width: 1200px;margin: 0 auto"
                 mode="horizontal"
                 background-color="#0aa1ed" @select="handleSelect"
        text-color="#fff" active-text-color="#fff">
          <el-menu-item v-for="c in categoryArr" :index="c.id">{{c.name}}</el-menu-item>
          <div style="float: right;position: relative;top: 10px">
            <el-input type="text" v-model="wd">
              <el-button slot="append" @click="search()" icon="el-icon-search"></el-button>
            </el-input>
          </div>

        </el-menu>
      </div>
    </el-header>
    <el-main style="width: 1200px;margin: 0 auto">
      <!--商品列表开始-->
      <el-row gutter="20">
        <el-col style="margin: 10px 0" span="6" v-for="p in productArr">
          <el-card>
            <a :href="'/detail.html?id='+p.id">
              <img :src="p.url" width="100%" height="233px">
            </a>
            <a :href="'/detail.html?id='+p.id">
              <p style="font-size: 14px;height: 30px">{{p.title}}</p>
            </a>
            <p>
              <b>￥{{p.price}}</b> <s>{{p.oldPrice}}</s>
              <span style="float: right">销量：{{p.saleCount}}件</span>
            </p>
          </el-card>
        </el-col>
      </el-row>
      <!--商品列表结束-->
    </el-main>
    <el-footer>
      <div style="height: 90px;background-image: url('imgs/wave.png')"></div>
      <div style="background-color: #282c30;text-align: center;
      color: #666;padding: 50px 0">
        <p>Copyright © *****科技有限公司版权所有 京ICP备******号-3 京公网安备 **********号</p>
      </div>
    </el-footer>
  </el-container>
</div>
</body>
<!--引入axios-->
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<!-- import Vue before Element -->
<script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
<!-- import JavaScript -->
<script src="https://cdn.staticfile.org/element-ui/2.15.9/index.min.js"></script>
<script>
 let v = new Vue({
    el: '#app',
    data: function() {
      return {
        productArr:[],
        categoryArr: [],
        wd:""
      }
    },
   methods:{
      search() {
        if(v.wd.trim()==''){
          v.$message.error("请输入搜索的关键词");
          return
        }
        //在结果页面根据关键词搜索，不用跳转页面，直接查询结果，显示在结果页面即可
        axios.get("/product/search?wd="+v.wd).then(function (response) {
          v.productArr = response.data;
        })
      },
     handleSelect(key,keyPath){
       console.log("分类id="+key)
       //已经在结果页面，点击菜单分类项，发起异步请求，根据点击的分类项id查询相关商品
       axios.get("/product/category?id="+key).then(function (response) {
         v.productArr = response.data;
       })
     }
   },
   created:function () {
      //判断地址栏中包含的是id还是wd
     if(location.search.indexOf("wd")!=-1){//代表用户是点击搜索按钮跳转过来的
       //发出搜索商品请求，           ?wd=羊毛衫
        axios.get("/product/search"+location.search).then(function (response) {
           v.productArr = response.data;
        })
     }else {//包含id，代表用户点击分类，跳转过来的
       //通过地址栏上的分类id查询相关商品
       axios.get("/product/category"+location.search).then(function (response) {
         v.productArr = response.data;
       })
     }
      //请求所有分类
     axios.get("/category/select").then(function (response) {
       v.categoryArr = response.data;
     })
   }
  })
</script>
</html>